package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;


import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.CarComment;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;

public class CarCommentDao implements ResultHandler<CarComment>{
	private MySqlDb db=MySqlDb.getInstance();
	/**
	 * 添加一条评价
	 * @param ccContent
	 * @param uid
	 * @param cid
	 * @return
	 * @throws SQLException
	 */
	public boolean save( String ccContent, int uid,int cid,int oid) throws SQLException {
		String sql = "insert into car_comment(cc_content,uid,cid,oid) values(?,?,?,?)";
		return db.executeUpdate(sql, new GlobalParamHandler(ccContent,uid,cid,oid)) > 0;
	}
	/**
	 * 通过订单oid查找评价
	 */
	public CarComment findByoid(int oid) throws SQLException {
		String sql = "select * from car_comment where oid = ?";
		return db.queryUninResult(sql, new GlobalParamHandler(oid), this);
	}
	/**
	 * 通过oid追加评价
	 */
	public boolean updateContent(String lei,int oid) throws SQLException{
		String sql="update car_comment set cc_content=? where oid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(lei,oid)) > 0;
	}
	/**
	 * 根据cid查询所有评价
	 * @return
	 * @throws SQLException
	 */
	public List<CarComment> getAll(int cid,int pageNo,int pageSize) throws SQLException{
		String sql = "select * from car_comment where cid=? order by cc_id desc limit "+pageNo+","+pageSize;
		return db.executeQuery(sql, new GlobalParamHandler(cid), this);
	}
	/**
	 * 获取cid的所有评价数量
	 * @param cid
	 * @return
	 * @throws SQLException
	 */
	public Long getAllCount(int cid) throws SQLException{
		String sql="select count(*) from car_comment where cid=?";
		return (Long) db.executeCountQuery(sql,new GlobalParamHandler(cid));
	}
	
	@Override
	public CarComment doHander(Map<String, Object> row) {
		CarComment cc=new CarComment();
		cc.setCcid(Integer.parseInt(row.get("cc_id")+"") );
		cc.setCcContent(row.get("cc_content")+"");
		cc.setCid(Integer.parseInt(row.get("cid")+"") );
		cc.setUid(Integer.parseInt(row.get("uid")+""));
		cc.setCcTime((Date)row.get("cc_time"));
		return cc;
	}

}
